# -*- encoding=utf8 -*-
'''自动发布闲置物品'''
__author__ = "v_jwjwchen"
import sys,os,argparse
sys.path.append(os.getcwd())
from auto_delivery_fun import *
from airtest.core.api import *
# parser = argparse.ArgumentParser(description='manual to this script')
# parser.add_argument('--devices', type=str, default = None)
# args = parser.parse_args()
# if args.devices:
#     devices_uri="android://127.0.0.1:5037/%s?cap_method=javacap&touch_method=adb"%(args.devices)
# else:
#     devices_uri="android:///"
# connect_device(devices_uri)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
x,y = poco.get_screen_size() #获取屏幕分辨率


#--------------初始化咸鱼app-----------------------    
package = "com.taobao.idlefish"
init_idle_fish(package)
#--------------提示升级，点击不升级-----------------------
sleep(15)
if poco(text='暂不升级').exists():
    poco(text='暂不升级').click()
#------------------------------------------------------------
i = 1
for count_time in range(40):
    start_app(package)
    #---------------点击闲置--------------------------------------
    wait_for_appearance_and_click(poco(text="卖闲置",touchable=False))
    if_exists_try_click(poco(text="卖闲置",touchable=False))
    #---------------发布闲置--------------------------------------
    wait_for_appearance_and_click(poco(textMatches="^发布闲置[\s\S]*",touchable=True))
#     wait_for_appearance_and_click(poco(textMatches="^拍照发布宝贝[\s\S]*",touchable=True))
    swipe((0.5*x,0.6*y),(0.5*x,0.2*y),duration=0.5)
    #---------------选择相册内容列表--------------------------------------
    photo_list = poco(text="双击选择当前图片",touchable=True,visible=True)
    photo_list_count = len(photo_list)
    sleep(2)
    #-------------相册选择自增器,每次都选择不同的照片------------------
    if i>photo_list_count: 
        i=1
    _index = photo_list_count - i
    i+=1
    #-------------自增器分割线------------------
    wait_for_appearance_and_click(photo_list[_index]) #选择相片
#     click_element_list(object_list,1,'desc') #选择相片
    #---------------点击下一步--------------------------------------
#     sleep(10)
    wait_for_appearance_and_click(poco(textMatches="^下一步.*",touchable=True))
#     sleep(2)
#     pu = (0.3*x,0.8*y)#宝贝被多人看到坐标
#     touch(pu)
    #---------------点击下一步--------------------------------------
    sleep(5)
    if poco(textMatches="^下一步.*",touchable=True).exists():
        poco(textMatches="^下一步.*",touchable=True).click()
    #---------------点击编辑框--------------------------------------
    sleep(3)
    poco(name="android.widget.EditText").click([0.1,0.1])
    #---------------输入内容--------------------------------------
    text(choice_comment())
    #---------------关联同款--------------------------------------
    if poco(textMatches="^关联淘宝同款[\s\S]*").exists():
        poco(textMatches="^关联淘宝同款[\s\S]*").click()
    #---------------设置价格--------------------------------------
    poco(textMatches="^价格[\s\S]*",touchable=True).click()
    wait_for_appearance_and_click(poco(text=YUAN,touchable=True))
    poco(text=".",touchable=True).click()
    poco(text=JIAO,touchable=True).click()
    poco(text=FEN,touchable=True).click()
    poco(textMatches="^.*包邮.*$",touchable=True).click()
    poco(text="确定",touchable=True).click()
    #---------------发布--------------------------------------
    poco(text="发布",touchable=True).click()
    #---------------发布成功，不领取曝光--------------------------------------
    sleep(5)
    try:
        wait_for_appearance_and_click(poco(name="android.widget.ImageView")[0])
    except:
        pass
    #---------------点击管理,设置自动回复--------------------------------------
    if auto_reply:
        wait_for_appearance_and_click(poco(text="管理",touchable=True))
        if poco(text="自动回复设置",touchable=True).exists():
            wait_for_appearance_and_click(poco(text="自动回复设置",touchable=True))
            poco(name="修改",touchable=True).click()
            poco(textMatches="^亲，我现在不在线.*",touchable=True).set_text(auto_reply_comment)
            swipe((0.5*x,0.4*y),(0.5*x,0.7*y),duration=0.5)
            swipe((0.5*x,0.4*y),(0.5*x,0.7*y),duration=0.5)
            poco(name="完成",touchable=True).click()
            wait_for_appearance_and_click(poco(name="开启宝贝自动回复",touchable=False))
            keyevent("BACK")
            sleep(2)
    #---------------返回主页--------------------------------------
    if poco(nameMatches="^宝贝是什么品牌.*$").exists():
        keyevent("BACK")
    keyevent("BACK")
    
    sleep(30)

